package com.minmin.testCases;

import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import java.util.List;

@Slf4j
public class TestPurchase extends TestCom {

    public TestPurchase(WebDriver driver) {
        super(driver);
    }

    public void testPurchase() throws InterruptedException {
        log.info("进入采购相关功能栏");
        eXpath("//div/span[text()=\"采购\"]").click();

        log.info("测试采购流程");
        testNewPurchase();
        log.info("--------------------------------------------");
        Thread.sleep(1000);

        log.info("测试分店要货流程");
        testNewEnquiry();
        log.info("--------------------------------------------");
        Thread.sleep(1000);

        log.info("测试供应商");
        testProvider();
        log.info("--------------------------------------------");
        Thread.sleep(1000);

        eXpath("//div/span[text()=\"采购\"]").click();
        Thread.sleep(1000);
    }

    private void testProvider() throws InterruptedException {
        log.info("进入供应商管理模块");
        eXpath("//ul/li/span[text()=\"供应商管理\"]").click();
        Thread.sleep(1000);
        log.info("新增供应商");
        eXpath("//button/span[text()=\"新增\"]").click();
        eXpath("//input[@placeholder=\"请选择结算方式\"]").click();
        Actions actions = new Actions(driver);
        actions.sendKeys(Keys.DOWN).perform();
        actions.sendKeys(Keys.DOWN).perform();
        actions.sendKeys(Keys.ENTER).perform();
        List<WebElement> elements = driver.findElements(By.xpath("//input[contains(@placeholder,\"请输入\")]"));
        for (int i = 1; i < elements.size(); i++) {
            if (i == 4) {
                elements.get(i).sendKeys("2643132195@qq.com");
                continue;
            }
            if (i == 7) {
                elements.get(i).sendKeys("123456789");
                continue;
            }
            elements.get(i).sendKeys("自动化创建供应商");
        }
        eXpath("//button/span[text()=\"确定\"]").click();
        if (A.alert(driver, "保存成功！")) {
            log.info("创建供应商成功");
        } else {
            log.info("创建供应商失败");
        }

        log.info("查询供应商");
        eXpath("//input[@placeholder=\"请输入自定义编码\"]").sendKeys("自动化创建供应商");
        eXpath("//button/span[text()=\"搜索\"]").click();
        if (driver.findElements(By.xpath("//span[text()=\"删除\"]")).size() == 1) {
            log.info("搜索供应商正常");
        } else {
            log.info("请检查搜索供应商功能");
        }
        log.info("删除供应商");
        eXpath("//span[text()=\"删除\"]").click();
        eXpath("//button/span[text()=\"确定\"]").click();
        if (A.alert(driver, "删除成功")) {
            log.info("删除供应商成功");
        } else {
            log.info("删除供应商失败");
        }
    }

    private void testNewPurchase() throws InterruptedException {
        log.info("进入采购管理模块");
        eXpath("//ul/li/span[text()=\"采购管理\"]").click();
        Thread.sleep(500);

        log.info("新增采购单");
        eXpath("//button/span[text()=\"新增\"]").click();
        eXpath("(//input[@placeholder=\"请选择供应商\"])[2]").click();
        Actions actions = new Actions(driver);
        actions.sendKeys(Keys.DOWN).build().perform();
        actions.sendKeys(Keys.ENTER).build().perform();
        Thread.sleep(500);
        eXpath("//span[text()=\" 选择商品 \"]").click();
        Thread.sleep(500);
        List<WebElement> elements = driver.findElements(By.xpath("//tbody/tr/td/div/label/span/span"));
        for (int i = 0; i < elements.size(); i++) {
            if (i > 3) break;
            elements.get(i).click();
        }
        eXpath("//button/span[text()=\"确定选择\"]").click();
        List<WebElement> elements1 = driver.findElements(By.xpath("//input[@placeholder=\"请输入\"]"));
        for (WebElement e : elements1) {
            e.clear();
            e.sendKeys("5");
        }
        eXpath("//button//span[text()=\" 确定 \"]").click();
        if (A.alert(driver, "新增成功")) {
            log.info("新增采购单成功");
        } else {
            log.info("新增采购单失败");
        }
        Thread.sleep(500);
        eXpath("(//button//span[text()=\" 审核 \"])[1]").click();
        Thread.sleep(500);
        eXpath("(//button//span[text()=\" 审核 \"])[last()]").click();
        eXpath("//input[@placeholder=\"请输入\"]").sendKeys("自动化测试采购单");
        eXpath("//button//span[text()=\"审核通过\"]").click();
        if (A.alert(driver, "操作成功")) {
            log.info("采购单审核成功");
        } else {
            log.info("采购单审核失败");
        }

        Thread.sleep(500);
        eXpath("(//button/span[text()=\" 到货单 \"])[1]").click();
        Thread.sleep(100);
        eXpath("//button/span[text()=\" 一键入库 \"]").click();
        Thread.sleep(100);
        eXpath("(//button/span[text()=\"确定\"])[last()]").click();
        if (A.alert(driver, "入库成功")) {
            log.info("采购流程完成");
        } else {
            log.info("采购入库失败,请检查！");
        }
        actions.sendKeys(Keys.ESCAPE).perform();
    }

    private void testNewEnquiry() throws InterruptedException {
        log.info("进入要货管理模块");
        eXpath("//ul/li/span[text()=\"要货单管理\"]").click();
        Thread.sleep(500);

        log.info("新增要货单");
        eXpath("//button/span[text()=\"新增\"]").click();
        Thread.sleep(500);
        List<WebElement> elements = driver.findElements(By.xpath("//tbody/tr/td/div/label/span/span"));
        for (int i = 0; i < elements.size(); i++) {
            if (i > 3) break;
            elements.get(i).click();
        }
//        eXpath("//button/span[text()=\"确定\"]").click();
        eXpath("//button/span[text()=\"确定选择\"]").click();

        List<WebElement> elements1 = driver.findElements(By.xpath("//input[@placeholder=\"请输入\"]"));
        for (WebElement e : elements1) {
            e.sendKeys("5");
        }
        eXpath("//button/span[text()=\" 提交审核 \"]").click();
        Thread.sleep(100);
        eXpath("//button/span[text()=\"刷新\"]").click();
        Thread.sleep(100);
        eXpath("(//span[text()=\" 审核 \"])[1]").click();
        Thread.sleep(100);
        eXpath("//button/span[text()=\"通过\"]").click();
        Thread.sleep(100);
        eXpath("//input[@placeholder=\"请输入\"]").sendKeys("自动化测试要货单");
        eXpath("(//button/span[text()=\"通过\"])[2]").click();
        Thread.sleep(500);
        eXpath("(//button/span[text()=\" 要货 \"])[1]").click();
        Thread.sleep(1000);
        eXpath("//button/span[text()=\"发起要货\"]").click();
        Thread.sleep(100);
        eXpath("//button/span[text()=\"确认\"]").click();
        Thread.sleep(100);
        if (A.alert(driver, "提交成功")) {
            log.info("分店创建要货单成功");
        } else {
            log.info("分店创建要货单失败");
        }
    }
}
